<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>observationset.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_labs_observe_observationset.js.html">observationset.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2012 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview A set of observations. This set provides a convenient
<a name="line17"></a> * means of observing many observables at once.
<a name="line18"></a> *
<a name="line19"></a> * This is similar in purpose to {@code goog.events.EventHandler}.
<a name="line20"></a> *
<a name="line21"></a> */
<a name="line22"></a>
<a name="line23"></a>goog.provide(&#39;goog.labs.observe.ObservationSet&#39;);
<a name="line24"></a>
<a name="line25"></a>goog.require(&#39;goog.array&#39;);
<a name="line26"></a>goog.require(&#39;goog.labs.observe.Observer&#39;);
<a name="line27"></a>
<a name="line28"></a>
<a name="line29"></a>
<a name="line30"></a>/**
<a name="line31"></a> * A set of observations. An observation is defined by an observable
<a name="line32"></a> * and an observer. The set keeps track of observations and
<a name="line33"></a> * allows their removal.
<a name="line34"></a> * @param {!Object=} opt_defaultScope Optional function scope to use
<a name="line35"></a> *     when using {@code observeWithFunction} and
<a name="line36"></a> *     {@code unobserveWithFunction}.
<a name="line37"></a> * @constructor
<a name="line38"></a> */
<a name="line39"></a>goog.labs.observe.ObservationSet = function(opt_defaultScope) {
<a name="line40"></a>  /**
<a name="line41"></a>   * @type {!Array.&lt;!goog.labs.observe.ObservationSet.Observation_&gt;}
<a name="line42"></a>   * @private
<a name="line43"></a>   */
<a name="line44"></a>  this.storedObservations_ = [];
<a name="line45"></a>
<a name="line46"></a>  /**
<a name="line47"></a>   * @type {!Object|undefined}
<a name="line48"></a>   * @private
<a name="line49"></a>   */
<a name="line50"></a>  this.defaultScope_ = opt_defaultScope;
<a name="line51"></a>};
<a name="line52"></a>
<a name="line53"></a>
<a name="line54"></a>/**
<a name="line55"></a> * Observes the given observer on the observable.
<a name="line56"></a> * @param {!goog.labs.observe.Observable} observable The observable to
<a name="line57"></a> *     observe on.
<a name="line58"></a> * @param {!goog.labs.observe.Observer} observer The observer.
<a name="line59"></a> * @return {boolean} True if the observer is successfully registered.
<a name="line60"></a> */
<a name="line61"></a>goog.labs.observe.ObservationSet.prototype.observe = function(
<a name="line62"></a>    observable, observer) {
<a name="line63"></a>  var success = observable.observe(observer);
<a name="line64"></a>  if (success) {
<a name="line65"></a>    this.storedObservations_.push(
<a name="line66"></a>        new goog.labs.observe.ObservationSet.Observation_(
<a name="line67"></a>            observable, observer));
<a name="line68"></a>  }
<a name="line69"></a>  return success;
<a name="line70"></a>};
<a name="line71"></a>
<a name="line72"></a>
<a name="line73"></a>/**
<a name="line74"></a> * Observes the given function on the observable.
<a name="line75"></a> * @param {!goog.labs.observe.Observable} observable The observable to
<a name="line76"></a> *     observe on.
<a name="line77"></a> * @param {function(!goog.labs.observe.Notice)} fn The handler function.
<a name="line78"></a> * @param {!Object=} opt_scope Optional scope.
<a name="line79"></a> * @return {goog.labs.observe.Observer} The registered observer object.
<a name="line80"></a> *     If the observer is not successfully registered, this will be null.
<a name="line81"></a> */
<a name="line82"></a>goog.labs.observe.ObservationSet.prototype.observeWithFunction = function(
<a name="line83"></a>    observable, fn, opt_scope) {
<a name="line84"></a>  var observer = goog.labs.observe.Observer.fromFunction(
<a name="line85"></a>      fn, opt_scope || this.defaultScope_);
<a name="line86"></a>  if (this.observe(observable, observer)) {
<a name="line87"></a>    return observer;
<a name="line88"></a>  }
<a name="line89"></a>  return null;
<a name="line90"></a>};
<a name="line91"></a>
<a name="line92"></a>
<a name="line93"></a>/**
<a name="line94"></a> * Unobserves the given observer from the observable.
<a name="line95"></a> * @param {!goog.labs.observe.Observable} observable The observable to
<a name="line96"></a> *     unobserve from.
<a name="line97"></a> * @param {!goog.labs.observe.Observer} observer The observer.
<a name="line98"></a> * @return {boolean} True if the observer is successfully removed.
<a name="line99"></a> */
<a name="line100"></a>goog.labs.observe.ObservationSet.prototype.unobserve = function(
<a name="line101"></a>    observable, observer) {
<a name="line102"></a>  var removed = goog.array.removeIf(
<a name="line103"></a>      this.storedObservations_, function(o) {
<a name="line104"></a>        return o.observable == observable &amp;&amp;
<a name="line105"></a>            goog.labs.observe.Observer.equals(o.observer, observer);
<a name="line106"></a>      });
<a name="line107"></a>
<a name="line108"></a>  if (removed) {
<a name="line109"></a>    observable.unobserve(observer);
<a name="line110"></a>  }
<a name="line111"></a>  return removed;
<a name="line112"></a>};
<a name="line113"></a>
<a name="line114"></a>
<a name="line115"></a>/**
<a name="line116"></a> * Unobserves the given function from the observable.
<a name="line117"></a> * @param {!goog.labs.observe.Observable} observable The observable to
<a name="line118"></a> *     unobserve from.
<a name="line119"></a> * @param {function(!goog.labs.observe.Notice)} fn The handler function.
<a name="line120"></a> * @param {!Object=} opt_scope Optional scope.
<a name="line121"></a> * @return {boolean} True if the observer is successfully removed.
<a name="line122"></a> */
<a name="line123"></a>goog.labs.observe.ObservationSet.prototype.unobserveWithFunction = function(
<a name="line124"></a>    observable, fn, opt_scope) {
<a name="line125"></a>  var observer = goog.labs.observe.Observer.fromFunction(
<a name="line126"></a>      fn, opt_scope || this.defaultScope_);
<a name="line127"></a>  return this.unobserve(observable, observer);
<a name="line128"></a>};
<a name="line129"></a>
<a name="line130"></a>
<a name="line131"></a>/**
<a name="line132"></a> * Removes all observations registered through this set.
<a name="line133"></a> */
<a name="line134"></a>goog.labs.observe.ObservationSet.prototype.removeAll = function() {
<a name="line135"></a>  goog.array.forEach(this.storedObservations_, function(observation) {
<a name="line136"></a>    var observable = observation.observable;
<a name="line137"></a>    var observer = observation.observer;
<a name="line138"></a>    observable.unobserve(observer);
<a name="line139"></a>  });
<a name="line140"></a>};
<a name="line141"></a>
<a name="line142"></a>
<a name="line143"></a>
<a name="line144"></a>/**
<a name="line145"></a> * A representation of an observation, which is defined uniquely by
<a name="line146"></a> * the observable and observer.
<a name="line147"></a> * @param {!goog.labs.observe.Observable} observable The observable.
<a name="line148"></a> * @param {!goog.labs.observe.Observer} observer The observer.
<a name="line149"></a> * @constructor
<a name="line150"></a> * @private
<a name="line151"></a> */
<a name="line152"></a>goog.labs.observe.ObservationSet.Observation_ = function(
<a name="line153"></a>    observable, observer) {
<a name="line154"></a>  this.observable = observable;
<a name="line155"></a>  this.observer = observer;
<a name="line156"></a>};
</pre>


</body>
</html>
